package org.apogee.mil.client.main.pages.trainee.forms;

import java.util.Date;
import java.util.List;

import org.apogee.mil.client.factories.ServerServiceFactory;
import org.apogee.mil.client.utils.Session;
import org.apogee.mil.client.widgets.DialogHelper;
import org.apogee.mil.shared.Question;
import org.apogee.mil.shared.User;

import br.com.freller.tool.client.Print;

import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.FormButtonBinding;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.TextArea;
import com.extjs.gxt.ui.client.widget.layout.FormData;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.rpc.AsyncCallback;

public class FinishedForm extends LayoutContainer {

	private User currentUser;
	private List<Question> wrong;
	private int correctQuestions;
	private int totalQuestions;
	private FormData data;
	private TextArea result;
	private float score;

	public FinishedForm(List<Question> wrong, int correctQuestions, int totalQuestions) {
		this.currentUser = ServerServiceFactory.getUser(Session.getSessionId());
		this.wrong = wrong;
		this.correctQuestions = correctQuestions;
		this.totalQuestions = totalQuestions;
	}

	public void onRender(Element parent, int pos) {
		super.onRender(parent, pos);
		
		data = new FormData("100%");


		score = ((float)correctQuestions / (float)totalQuestions) * 100;

		FormPanel panel = new FormPanel();
		panel.setHeading("Test Results");
		panel.setFieldWidth(1);
		panel.setSize(400, 250);
		panel.setHideLabels(true);

		result = new TextArea();
		result.setHeight(150);
		result.setReadOnly(true);
		String output = "Total Correct: " +correctQuestions + " Total Questions: " + totalQuestions +"\nPercent: " 
				+ score +"%";

		
		if (score < currentUser.getTestMinScore()) {
			output += "\nSorry " + currentUser.getUserName() +", you did not pass";
		} else {
			output += "\nCongratulations " + currentUser.getUserName() +", You Passed";
		}
		
		result.setValue(output);
		
		Button b = new Button("Print");
		b.addSelectionListener(new SelectionListener<ButtonEvent>() {
			
			@Override
			public void componentSelected(ButtonEvent ce) {
				String printOutput = "<center><p style='color:blue; font-size:24pt;'>Results</p>" +
						"<br/><p style='color:#5F9EA0; font-size:18pt;'</p><br/>User: "+ currentUser.getUserId() + "&nbsp;" +
						currentUser.getUserName() + "<br/>Test Taken:&nbsp;" + currentUser.getCurrentTest() + 
						"<br/>Total Correct: " + correctQuestions + " Total Questions: " + totalQuestions +"<br/>Percent: " 
						+ score +"%";
				
				if (score < currentUser.getTestMinScore()) {
					printOutput += "<br/><p style='color:red; font-size:18pt;'>Sorry " + currentUser.getUserName() +", you did not pass</p>";
				} else {
					printOutput += "<br/><p style='color:green; font-size:18pt;'>Congratulations " + currentUser.getUserName() +", You Passed</p>";
							
				}
				printOutput += "<br/><p style='color:black; font-size:8pt;'>Date Taken: " + new Date().toString() +"</p></center>";
				Print.it(printOutput);
				
			}
		});
		
		
		panel.add(result, data);
		
		Button retake = new Button("Retake Missed Questions");
		retake.addSelectionListener(new SelectionListener<ButtonEvent>() {
			
			@Override
			public void componentSelected(ButtonEvent ce) {
				currentUser.setRedo(false);
				DialogHelper dialog = new DialogHelper("Test", new QuestionForm(wrong));
				dialog.show();
				getParent().setVisible(false);
				
			}
		});
		
		Button review = new Button("Review Missed Questions");
		review.addSelectionListener(new SelectionListener<ButtonEvent>() {
			
			@Override
			public void componentSelected(ButtonEvent ce) {
				DialogHelper dialog = new DialogHelper("Review", new ReviewForm(wrong));
				dialog.show();
				
			}
		});
		
		if (wrong.size() == 0) {
			retake.disable();
			retake.disableEvents(true);
			review.disable();
			review.disableEvents(true);
		}
		
		
		panel.addButton(b);  
		panel.addButton(retake);
		panel.addButton(review);
		panel.setButtonAlign(HorizontalAlignment.CENTER);
	      
	    FormButtonBinding binding = new FormButtonBinding(panel);  
	    binding.addButton(b);
	    binding.addButton(retake);
	    binding.addButton(review);
		
		add(panel);
		layout();
		
		submitResults();
	}

	private void submitResults() {
		if (currentUser.getRedo() == false) {
			return;
		}
		
		ServerServiceFactory.getTraineeService(currentUser.getSessionId()).addResults(currentUser.getUserId(), currentUser.getCurrentTest(), 
				score, new AsyncCallback<Boolean>() {
					
					@Override
					public void onSuccess(Boolean result) {
						
					}
					
					@Override
					public void onFailure(Throwable caught) {
						MessageBox.alert("Error", "Unable to record score", null);
						
					}
				});
		
	}

}
